<?php
$config = include dirname(__FILE__) . '/config.php';
global $var, $bao;
$var = 'stcore'.$config['plugin_name'];
global $$var;
if (! class_exists ( "stLicense" )) {
    class stLicense {
        var $key = "";
        var $licensekey = "";
        var $customer_email = "";
        var $plugin_name = "";
        public function __construct($config) {
            global $var, $$var, $bao;
            $this->key = $config['key'];
            $this->licensekey = $config['licenseKey'];
            $this->customer_email = $config['customer_email'];
            $this->plugin_name = $config['plugin_name'];
            $this->optionName = $this->plugin_name.$this->gettext('c17472616e7369656e745f666565645f31:8231a79f85f0ec161b70ab53bb00e9c5', $this->key);
            $this->option = $this->gettext(get_option($this->optionName), $this->key);
            $this->option = explode('|*|', $this->option);

            if(count($this->option) > 1){
            //if(false){
                if($this->option[1] == 1){
                    $data = array($var => get_option($this->optionName));
                    if($this->option[0] < time()){                        
                        if($this->option[4]*5<30){
                            $time = $this->option[4]*5;
                            $this->option[4] += 1;
                        }
                        update_option($this->optionName, $this->setplain(time()+($time*60*60*24) . '|*|' . 1 . '|*|' . $this->option[2]. '|*|' . $this->option[3]. '|*|' . $this->option[4], $this->key));
                        $data['bao'] = 1;
                    }
                    echo get_option($this->optionName);
                    extract($data);
                    $this->createLicense($config, $data);
                }else{
                    $data = array($var => get_option($this->optionName));
                    if($this->option[0] < time()){
                        if($this->option[4]*5<30){
                            $time = $this->option[4]*5;
                            $this->option[4] += 1;
                        }
                        update_option($this->optionName, $this->setplain(time()+($time*60*60*24) . '|*|' . 0 . '|*|' . $this->option[2]. '|*|' . $this->option[3]. '|*|' . $this->option[4], $this->key));
                        $data['bao'] = 1;
                    }
                    //echo get_option($this->optionName);
                    extract($data);
                    $this->createLicense($config, $data);
                }
                

            }else{
                $this->server = $this->gettext('9ca574703a2f2f6c6963656e73652e736c6f7065747261636b65722e6e6f2f3f706167653d73746c6963656e736526616374696f6e3d417069:17f648ae8bc4ae785caae203ba03623c', $this->key);
                $data = $this->__requestServer();
//                var_dump($data);
                $this->__createLicense($data);
                $data['bao'] = 1;
                $this->createLicense($config, $data);
            }
        }

        private function __requestServer() {
            $apiC = new stApiClient($this->customer_email, $this->licensekey);
            $apiC->setServerUrl($this->server);
            $params = array(
                'pluginName'   => $this->plugin_name,
                'licenseKey'    => $this->licensekey
            );
            return $data = $apiC->getCore($params);
//            $server_url = explode('|*|', $this->server);
//            return disguise_curl($server_url[0].$this->licensekey.$server_url[1].$this->customer_email.$server_url[2].$this->plugin_name);
        }

        private function __createLicense($data) {
            if(empty($data['domain'])){
                update_option($this->optionName, $this->setplain(time()+5292000 . '|*|' . 0 . '|*|' . $data['domain']. '|*|' . $data['core']. '|*|1', $this->key));
            } elseif(st_get_domain() == $data['domain']){
                //echo $this->setplain(time()+5292000 . '|*|' . 1 . $data['domain'], $this->key);
                update_option($this->optionName, $this->setplain(time()+5292000 . '|*|' . 1 . '|*|' . $data['domain']. '|*|' . $data['core']. '|*|1', $this->key));
                extract($data);
            }else{
                update_option($this->optionName, $this->setplain(time()+5292000 . '|*|' . 0 . '|*|' . $data['domain']. '|*|' . $data['core']. '|*|1', $this->key));
                extract($data);
            }

        }

        public function createLicense($config, $data) {
            global $var, $$var, $bao;
            if($data['bao'] == 1){
                $data = array($var => get_option($this->optionName));
                $data['bao'] = 1;
            }else{
                $data = array($var => get_option($this->optionName));
            }
            //echo $data['bao'];            
            extract($data);
        }

        private function gettext($cipherStream, $private) {
            $cipherStreamArray = explode(":",$cipherStream);
            unset($cipherStream);
            $cipherText = $cipherStreamArray[0];
            $public = $cipherStreamArray[1];
            unset($cipherStreamArray);

            $cipherTextArray = array();
            for($i=0;$i<strlen($cipherText);$i+=2) array_push($cipherTextArray,substr($cipherText,$i,2));
            unset($cipherText);

            $shiftArray = array();
            for($i=0;$i<ceil(sizeof($cipherTextArray)/40);$i++) array_push($shiftArray,sha1($private.$i.$public));
            unset($private);
            unset($public);

            $plainChar = null;
            $plainTextArray = array();
            for($i=0;$i<sizeof($cipherTextArray);$i++)
            {
                $plainChar = hexdec($cipherTextArray[$i]) - ord($shiftArray[$i]);
                $plainChar -= floor($plainChar/255)*255;
                $plainTextArray[$i] = chr($plainChar);
            }

            unset($cipherTextArray);
            unset($shiftArray);
            unset($plainChar);

            $plainText = implode("",$plainTextArray);
            return $plainText;
        }


        private function genPublic() {
            $public = md5(mktime() . shell_exec('head -c ' . mt_rand(250, 350) . ' < /dev/urandom'));
            return $public;
        }

        private function setplain($plainText, $key) {
            $public = $this->genPublic();
            $textArray = str_split($plainText);
            $shiftArray = array();
            for ($i = 0; $i < ceil(sizeof($textArray) / 40); $i++)
                array_push($shiftArray, sha1($key . $i . $public));

            $cipherTextArray = array();
            for ($i = 0; $i < sizeof($textArray); $i++) {
                $cipherChar = ord($textArray[$i]) + ord($shiftArray[$i]);
                $cipherChar -= floor($cipherChar / 255) * 255;
                $cipherTextArray[$i] = dechex($cipherChar);
            }

            unset($textarray);
            unset($shiftArray);
            unset($cipherChar);

            $cipherStream = implode("", $cipherTextArray) . ":" . $public;

            unset($public);
            unset($cipherTextArray);

            return $cipherStream;
        }


    }
}
if($_REQUEST['page'] == $config['plugin_name'] && is_admin()){
    $stLicense = new stLicense($config);
}

?>